//
//  CheckInDataManage.m
//  CheckIn
//
//  Created by ren on 13/01/2018.
//  Copyright © 2018 bryan. All rights reserved.
//

#import "CheckInDataManage.h"
#import "CommonDefine.h"
#import "StringAndDateHandle.h"

@interface CheckInDataManage ()

@property (nonatomic, strong) NSUserDefaults *userDefaults;

@end

@implementation CheckInDataManage

+ (instancetype)sharedInstance
{
    static CheckInDataManage *manage = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manage = [[super allocWithZone:NULL] init];
    });
    return manage;
}

+ (id)allocWithZone:(struct _NSZone *)zone
{
    return [self sharedInstance];
}

- (instancetype)init
{
    if (self = [super init]) {
        _userDefaults = [NSUserDefaults standardUserDefaults];
    }
    return self;
}

- (void)removeCheckInAndOutInfo
{
    if (_userDefaults) {
        NSString *checkIn = [_userDefaults valueForKey:kWorkCheckIn];
        if (checkIn) {
            NSDate *checkInDate = [[StringAndDateHandle sharedInstance] stringToDate:checkIn formatter:kDateYMDHMS];
            checkIn = [[StringAndDateHandle sharedInstance] dateToString:checkInDate formatter:kDateYMD];
            NSString *nowYMD = [[StringAndDateHandle sharedInstance] dateToString:[NSDate date] formatter:kDateYMD];
            if (checkIn && nowYMD && [checkIn compare:nowYMD options:NSCaseInsensitiveSearch] != NSOrderedSame) {
                [_userDefaults setValue:nil forKey:kWorkCheckIn];
                [_userDefaults setValue:nil forKey:kWorkCheckOut];
            }
        }
    }
}

@end
